'''
@author: Alexandre Ribeiro de Souza
'''

from cherrypy import expose
from subprocess import call
import cherrypy
import os
import shlex
import thread

path = os.path.join('/root')

def call_process(command, running_dir):
    current_dir = os.getcwd()
    os.chdir(running_dir)
    call(shlex.split(command))#, stdout=open('log.txt', 'w'))
    os.chdir(current_dir)

def create_databag(client, site):
    databag = []
    databag.append('{\n')
    databag.append('  "id": "%(client)s",\n' % { 'client': client })
    databag.append('  "url": "www.%(site)s",\n' % { 'site': site })
    databag.append('  "url_alias": [ "%(site)s" ]\n' % { 'site': site })
    databag.append('}')
    databag_file = open('/tmp/%(client)s.json' % { 'client': client }, 'w')
    databag_file.writelines(databag)
    databag_file.flush()
    databag_file.close()

class SaaS:
    
    @expose
    def index(self):
        return "Lumis SaaS"
    
    @expose
    def createdev(self, client, site):
        create_databag(client, site)
        
        cmd = 'knife data bag from file client /tmp/%(client)s.json' % { 'client': str(client) }
        call_process(cmd, path)
        
        cmd = 'knife ec2 server create -i /root/.chef/knife-keypair.pem -x root -r "role[saas], " -G saas -E saas-dev -N saas-%(client)s-dev -I ami-4427fd2d' % { 'client': str(client) }
        call_process(cmd, path)
        
        return "finished"
    
    @expose
    def create(self, client, site):
        create_databag(client, site)
        
        cmd = 'knife data bag from file client /tmp/%(client)s.json' % { 'client': str(client) }
        call_process(cmd, path)
        
        cmd = 'knife ec2 server create -i /root/.chef/knife-keypair.pem -x root -r "role[saas], " -G saas -E saas-db -N saas-%(client)s-db -I ami-7a5a8013' % { 'client': str(client) }
        call_process(cmd, path)
        
        cmd = 'knife ec2 server create -i /root/.chef/knife-keypair.pem -x root -r "role[saas], " -G saas -E saas-app -N saas-%(client)s-app -I ami-4427fd2d' % { 'client': str(client) }
        call_process(cmd, path)
        
        return "finished"
    
    @expose
    def createapp(self, client, sufix):
        cmd = 'knife ec2 server create -i /root/.chef/knife-keypair.pem -x root -r "role[saas], " -G saas -E saas-app -N saas-%(client)s-app-%(sufix)s -I ami-4427fd2d' % { 'client': str(client), 'sufix': str(sufix) }
        call_process(cmd, path)
        
        cmd = 'knife ssh -i /root/.chef/knife-keypair.pem -x root -a ec2.public_hostname "name:saas-%(client)s-app*" "sudo chef-client"' % { 'client': str(client) }
        call_process(cmd, path)
        
        return "finished"
    
    @expose
    def restore(self, client, date):
        cmd = 'knife ssh -i /root/.chef/knife-keypair.pem -x root -a ec2.public_hostname "name:saas-%(client)s-db" "/mnt/backup/restore.sh %(date)s"' % { 'client': str(client), 'date': str(date) }
        call_process(cmd, path)
        
        cmd = 'knife ssh -i /root/.chef/knife-keypair.pem -x root -a ec2.public_hostname "name:saas-%(client)s-app*" "reboot"' % { 'client': str(client) }
        call_process(cmd, path)
        
        return "restored"

import os.path
conf = os.path.join(os.path.dirname(__file__), 'saas.conf')

if __name__ == '__main__':
    cherrypy.quickstart(SaaS(), config=conf)
